<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Requests\GoodsPostRequest;
use App\Http\Controllers\Controller;
use App\models\Goods;

class GoodsController extends Controller
{
    //添加页面
    public function getAdd()
    {	
    	//分类处理
    	$cates = DB::table('cates')->get();
    	$cates = CatesController::getCates($cates);
    	return view('goods.add',['cates'=>$cates]);
    }

    public function postInsert(GoodsPostRequest $request)
    {
    	// dd($request->all());
    	
    	//实例化模型对象
    	$goods = new Goods;

    	$goods->title = $request->input('title');
    	$goods->price = $request->input('price');
    	$goods->kucun = $request->input('kucun');
    	$goods->content = $request->input('content');
    	$goods->cate_id = $request->input('cate_id');
    	$goods->status = $request->input('status');
    	$goods->pic = UaddController::upload($request);
    	// dd($goods);
    	//数据添加
    	$res = $goods->save();
    	// dd($res);
    	if($res){
    		return redirect('/admin/goods/index')->with('success','添加成功');
    	}else{
    		return back()->with('error','数据添加失败');
    	}
    }

    //商品显示列表
    public function getIndex(Request $request)
    {	
    	$num = $request->input('num',10);

    	if($request->input('keywords')){
    		$goods = DB::table('goods')->where('title','like','%'.$request->input('keywords').'%')->paginate($num);
    	}else{
    		$goods = DB::table('goods')->paginate($num);
    	}
    	
    	$cates = DB::table('cates')->get();
    	$cates = CatesController::getCates($cates);
    	return view('goods.index',['goods'=>$goods,'request'=>$request->all(),'cates'=>$cates]);
    }

    //商品删除
    public function getDelete(Request $request)
    {
    	// dd($request->all());
    	$id = $request->input('id');
    	$res = DB::table('goods')->where('id',$id)->delete();
    	if($res){
    		return redirect('/admin/goods/index')->with('success','删除成功');
    	}else{
    		return back()->with('error','数据删除失败');
    	}
    }

    //修改商品
    public function getEdit(Request $request)
    {
    	$id = $request->input('id');
    	$goods = DB::table('goods')->where('id',$id)->first();
    	$cates = DB::table('cates')->get();
    	$cates = CatesController::getCates($cates);

    	//解析模板
    	return view('goods.edit',['goods'=>$goods,'cates'=>$cates]);
    }
    //数据修改
    public function postUpdate(Request $request)
    {
    	// dd($request->all());
    	//实例化模型对象
    	$goods = new Goods;
    	$goods = $goods->find($request->input('id'));

    	$goods->title = $request->input('title');
    	$goods->price = $request->input('price');
    	$goods->kucun = $request->input('kucun');
    	$goods->content = $request->input('content');
    	$goods->cate_id = $request->input('cate_id');
    	$goods->status = $request->input('status');
    	if($request->hasFile('pic')){
    	    $goods['pic'] = self::upload($request);
        }

        // dd($goods);
        $res = $goods->save();
        if($res){
    		return redirect('/admin/goods/index')->with('success','修改成功');
    	}else{
    		return back()->with('error','数据修改失败');
    	}
    }
}
